using System;
using System.Collections.Generic;
using System.Web;
using eShop.Model;
using eShop.Business;

namespace eShop.Business
{
    public class SessionManager
    {
        public static string SearchKeyword
        {
            get
            {
                return (string)System.Web.HttpContext.Current.Session[SEARCH_KEYWORDS];
            }
            set 
            { 
                System.Web.HttpContext.Current.Session[SEARCH_KEYWORDS] = value; 
            }
        }

        public static string SearchItemNumber
        {
            get
            {
                string data = Convert.ToString(System.Web.HttpContext.Current.Session[SEARCH_ITEM_NUMBER]);
                System.Web.HttpContext.Current.Session[SEARCH_ITEM_NUMBER] = string.Empty;
                return data;
            }
            set { System.Web.HttpContext.Current.Session[SEARCH_ITEM_NUMBER] = value; }
        }

        public static Address ShippingAddress
        {
            get
            {
                if (System.Web.HttpContext.Current.Session[SHIPPING_ADDRESS] != null)
                    return (Address)System.Web.HttpContext.Current.Session[SHIPPING_ADDRESS];
                return null;
            }
            set { System.Web.HttpContext.Current.Session[SHIPPING_ADDRESS] = value; }
        }

        public static string OrderNumber
        {
            get
            {
                if (System.Web.HttpContext.Current.Session[ORDER_NUMBER] != null)
                    return (string)System.Web.HttpContext.Current.Session[ORDER_NUMBER];
                return null;
            }
            set { System.Web.HttpContext.Current.Session[ORDER_NUMBER] = value; }
        }

        public static Int32 OrderID
        {
            get
            {
                if (System.Web.HttpContext.Current.Session[ORDER_ID] != null)
                    return Convert.ToInt32(System.Web.HttpContext.Current.Session[ORDER_ID]);
                return -1;
            }
            set { System.Web.HttpContext.Current.Session[ORDER_ID] = value; }
        }

        public static CheckoutSteps CheckoutSteps
        {
            get
            {
                if (System.Web.HttpContext.Current.Session[CHECKOUT_STEPS] != null)
                    return (CheckoutSteps)System.Web.HttpContext.Current.Session[CHECKOUT_STEPS];
                return CheckoutSteps.OtherPages;
            }
            set { System.Web.HttpContext.Current.Session[CHECKOUT_STEPS] = value; }
        }

        // Property used for Shopping cart
        public static List<Product> RecentlyViewedProducts
        {
            get
            {
                if (System.Web.HttpContext.Current.Session[RECENTLY_VIEWED_PRODUCTS] != null)
                    return (List<Product>)System.Web.HttpContext.Current.Session[RECENTLY_VIEWED_PRODUCTS];

                return new List<Product>();
            }
            set 
            {
                System.Web.HttpContext.Current.Session[RECENTLY_VIEWED_PRODUCTS] = value; 
            }
        }

        // Property representing Logged in user of the site
        public static User LoggedUser
        {
            get { return (User)System.Web.HttpContext.Current.Session[SITE_USER]; }
            set { System.Web.HttpContext.Current.Session[SITE_USER] = value; }
        }

        // Read Only property, returns whether the user has logged-in  in the site
        public static bool IsUserLoggedIn
        {
            get { return (System.Web.HttpContext.Current.Session[SITE_USER] != null); }
        }

        // Property representing Logged in admin of the site
        public static User LoggedAdminUser
        {
            get { return (User)System.Web.HttpContext.Current.Session[SITE_ADMIN_USER]; }
            set { System.Web.HttpContext.Current.Session[SITE_ADMIN_USER] = value; }
        }

        // Read Only property, returns whether the admin has logged-in  in the site
        public static bool IsAdminUserLoggedIn
        {
            get { return (System.Web.HttpContext.Current.Session[SITE_ADMIN_USER] != null); }
        }

        // Property used for returning to a page requested before authentication
        // when authentication is complete
        public static string ReturnUrl
        {
            get { return (string)System.Web.HttpContext.Current.Session[RETURN_URL]; }
            set { System.Web.HttpContext.Current.Session[RETURN_URL] = value; }
        }


        // Constructors
        public SessionManager() { }

        private const string RETURN_URL = "ReturnUrl";
        private const string CURRENT_ADMIN_USER = "CurrentAdminUser";
        private const string CURRENT_USER = "CurrentUser";
        private const string SITE_ADMIN_USER = "SiteAdminUser";
        private const string SITE_USER = "SiteUser";

        private const string SEARCH_KEYWORDS = "Search_Keywords";
        private const string SEARCH_ITEM_NUMBER = "Search_Item_Number";
        private const string FAVORITES_LIST = "Favorites_List";
        private const string PRODUCT_SHIPPING = "Product_Shipping";
        private const string STATE_TAX = "State_Tax";
        private const string SHIPPING_ADDRESS = "Shipping_Address";
        private const string ORDER_NUMBER = "Order_Number";
        private const string ORDER_ID= "Order_ID";
        private const string CHECKOUT_STEPS = "CheckOut_Steps";
        private const string RECENTLY_VIEWED_PRODUCTS = "RecentlyViewedProducts";
    }
}

